﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.Security;

namespace Shool
{
    public partial class Register : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {

        }

        protected void ButtonRegister_Click(object sender, EventArgs e)
        {
            if (TextBoxPassword.Text == TextBoxPasswordRetype.Text)
            {
                try
                {
                    Membership.CreateUser(TextBoxUsername.Text, TextBoxPassword.Text);
                    if (!Roles.RoleExists(DropDownRole.Text))
                    {
                        Roles.CreateRole(DropDownRole.Text);
                    }
                    Roles.AddUserToRole(TextBoxUsername.Text, DropDownRole.Text);
                    LabelErrorInRegister.Text = String.Empty;

                    FormsAuthentication.RedirectFromLoginPage(TextBoxUsername.Text, false);                    
                }
                catch (Exception)
                {
                    LabelErrorInRegister.Text = "Username unavailable!";
                }
            }
            else
            {
                LabelErrorInRegister.Text = "Passwords don't match!";
            }
        }
    }
}